package sun.it.web;

import com.alibaba.druid.util.JdbcUtils;
import org.apache.commons.beanutils.BeanUtils;
import sun.it.domain.User;
import sun.it.service.UserService;
import sun.it.service.impl.UserServiceimpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/regisServlet")
public class RegisServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String enpassword = request.getParameter("enpassword");
        User user=new User();
        Map<String, String[]> map = request.getParameterMap();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {

        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        UserService service=new UserServiceimpl();
        if(password.equals(enpassword)){
            int regised = service.Regised(user);
            if(regised==1){
                response.sendRedirect(request.getContextPath()+"/login.jsp");
            }else {
                request.setAttribute("re_error","账号重复");
                request.getRequestDispatcher("sign.jsp").forward(request,response);
            }
        }else {
            request.setAttribute("re_error","密码不一样");
            request.getRequestDispatcher("sign.jsp").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
    }
}
